library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.0 v dplyr 1.0.5
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
covid19 <- read_csv("data/covid19-daily-cases.csv")
## Parsed with column specification:
## cols(
## country_region = col_character(),
## date = col_date(format = ""),
## confirmed = col_double()
## )
covid19
covid19 %>%
ggplot(aes(
x = date,
y = confirmed,
colour = country_region)) +
geom_line() +
guides(colour = FALSE)
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.

# Fit with log scale so it becomes normal
covid19 %>%
ggplot(aes(
x = date,
y = log10(confirmed),
colour = country_region)) +
geom_line() +
guides(colour = FALSE)
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.

#change scale for log
covid19 %>%
ggplot(aes(
x = date,
y = confirmed,
colour = country_region)) +
geom_line() +
guides(colour = FALSE) +
scale_y_log10()
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
## Warning: Transformation introduced infinite values in continuous y-axis

# sorting the data by countries
covid19_rel <- covid19 %>%
group_by(country_region) %>%
mutate(days = as.numeric(date - min(date))) %>%
ungroup()
covid19_rel
#graph for countries and days since march 1
world <- covid19_rel %>%
ggplot(aes(
x = days,
y = confirmed,
colour = country_region)) +
geom_line() +
scale_y_log10() +
guides(colour = FALSE)
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
world
## Warning: Transformation introduced infinite values in continuous y-axis

# nz highlighted out of all countries
covid19_nz <- covid19_rel %>%
filter(country_region == "New Zealand")
p_nz <- covid19_rel %>%
ggplot(aes(x = days, y = confirmed,
group = country_region)) +
geom_line(colour = "grey", alpha = 0.5) +
geom_line(colour = "#238b45", size = 1, data = covid19_nz) +
scale_y_log10() +
guides(colour = FALSE)
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
p_nz
## Warning: Transformation introduced infinite values in continuous y-axis

#changed labels
p_nz <- p_nz +
geom_label(aes(
x = max(days), y = max(confirmed),
label = country_region), data = covid19_nz,
colour = "#238b45", nudge_x = 3, nudge_y = .5)
p_nz
## Warning: Transformation introduced infinite values in continuous y-axis

p_nz <- p_nz +
scale_y_log10(labels = scales::label_comma()) +
xlim(c(0, 100))
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.
p_nz
## Warning: Transformation introduced infinite values in continuous y-axis

p_nz <- p_nz +
labs(
x = "Days since March 1",
y = "Confirmed cases (on log10)",
title = "Worldwide coronavirus confirmed cases",
subtitle = "highlighting New Zealand",
caption = "Data source: John Hopkins University, CSSE"
)
p_nz
## Warning: Transformation introduced infinite values in continuous y-axis

#make interactive
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
ggplotly(p_nz)
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomLabel() has yet to be implemented in plotly.
## If you'd like to see this geom implemented,
## Please open an issue with your example code at
## https://github.com/ropensci/plotly/issues